﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <link href="//unpkg.com/layui@2.10.1/dist/css/layui.css" rel="stylesheet">
    <style>
        .navbar {
            display: none;
        }
    </style>
</head>
<body>
    <!--查询-->
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <input type="text" placeholder="请输入体检套餐名称" name="packageName" class="layui-input"> <!-- 修改提示文本和参数名称为packageName -->
            </div>
            <button class="layui-btn layui-bg-blue" lay-submit lay-filter="search"> 查询</button>
            <button type="button" class="layui-btn layui-btn-normal" id="addBtn">新增</button>

        </div>
    </form>
    <!-- 表格-->
    <table class="layui-hide" id="role-table"></table>
    <script src="//unpkg.com/layui@2.10.1/dist/layui.js"></script>
    <script>
        layui.use(['table', 'jquery', 'layer', 'laypage', 'form', 'laydate'],

            function () {
                var table = layui.table;
                var $ = layui.jquery;
                var layer = layui.layer;
                var laypage = layui.laypage;
                var form = layui.form;
                var laydate = layui.laydate;

                var totalCount=0;

                // 日期时间选择器
                laydate.render({
                    elem: '#ID-laydate-type-datetime',
                    type: 'datetime'
                });
                // 初始化表格
                table.render({
                    elem: '#role-table',
            id: 'HealthCheckPackage',
                    height: '500',
                    cols: [[
                        { field: 'healthCheckPackageId', width: 80, sort: true, fixed: 'left', title: 'ID' },
                        { field: 'packageNo', width: 130, title: '体检套餐编号' },
                { field: 'packageName', width: 130, title: '体检套餐名称' },
                { field: 'packageType', title: '体检套餐类型', width: 130 },
                { field: 'applicableToHospitals', width: 130, title: '适用院区' },
                { field: 'packagePrice', width: 130, title: '套餐价格' },
                { field: 'packages', width: 130, title: '套餐介绍' },
                { field: 'courseDescription', width: 130, title: '套餐类型' },
                        { field: 'operation', title: '操作', toolbar: '#barDemo' },
                    ]],
                    page: true, // 开启分页
                    limit: 15, // 默认每页显示15条
                    limits: [2, 15, 30, 50] // 可选每页显示条数
                });
                function loadRoleData(PageIndex, PageSize) {
                    layer.load(2);
                    var packageName = $('[name="packageName"]').val(); // 修改获取参数名称为packageName
                    var params = {
                        PageIndex: PageIndex || 1, // 默认第一页
                        PageSize: PageSize || 15,   // 默认每页15条
                        packageName: packageName // 修改参数名称为packageName
                    }
                    $.ajax({
                        //url: 'https://localhost:7171/api/HealthCheckPackage/GetHealthCheckPackage',
                        url: `@ViewBag.BaseUrl/T5/HealthCheckPackage/GetHealthCheckPackage`,
                        data: params,
                        type: 'get',
                        dataType: 'json',
                        success: function (res) {
                            console.log(res.totalCount);
                            layer.closeAll('loading');
                            if (res.code === 0) {
                                totalCount=res.totalCount;
                        table.reload('HealthCheckPackage', {
                                    data: res.data,
                                    count: res.totalCount,
                                    limit: PageSize || 15,
                                    curr: PageIndex || 1,
                                });

                                //分页
                                laypage.render({
                                    count: res.totalCount, // 数据总数
                                    curr: PageIndex,
                                    limit: PageSize,
                                    limits: [2, 15, 30, 50],
                                    layout: ['count', 'prev', 'page', 'next', 'limit','refresh','skip'],
                                    jump: function (obj, first) {
                                        if (!first) {
                                            loadRoleData(obj.curr, obj.limit);
                                        }
                                    }
                                });

                            } else {
                                layer.msg('获取数据失败: ' + (res.message || '未知错误'), { icon: 2 });
                            }
                        },
                        error: function (xhr, status, error) {
                            layer.closeAll('loading');
                            layer.msg('请求失败: ' + error, { icon: 2 });
                            console.error('Ajax请求失败:', xhr.responseText);
                        }
                    });
                }
                // 页面加载完成后获取数据
                loadRoleData(1, totalCount);

                // 表单提交事件
                form.on('submit(search)', function () {
                    loadRoleData();
                    return false;
                });

                // 新增按钮点击事件
                $('#addBtn').on('click', function () {
                    layer.open({
                        type: 1,
                        title: '新增',
                        area: ['60%', '60%'],
                        content: $('#addDialog'),
                        success: function () {
                            // 重置表单
                            $('#addForm')[0].reset();
                            // 初始化日期选择器
                            laydate.render({
                                elem: '#date',
                                trigger: 'click'
                            });
                            form.render();
                        }
                    });
                });
                // 新增表单提交
                form.on('submit(addSubmit)', function (data) {
                    var formData = data.field;
                    var postData = {
                        messageId: 0,
                        title: formData.title,
                        content: formData.content,
                        createName: formData.createName,
                        createTime: formData.createTime,
                        statue: $("#Status").prop('checked'),
                        isDeleted: false
                    };

                    layer.load(2);
                    $.ajax({
                        //url: 'https://localhost:7027/api/Message/CreateMessage',
                        url: `@ViewBag.BaseUrl/T5/Message/CreateMessage`,
                        type: 'post',
                        contentType: 'application/json',
                        data: JSON.stringify(postData),
                        dataType: 'json',
                        success: function (res) {
                            layer.closeAll('loading');
                            if (res.code == 100) {
                                layer.alert(res.test, {
                                    title: '操作成功',
                                    icon: 1,
                                    btn:['确定'],
                                    yes: function (index, layero) {
                                        document.getElementById("addForm").reset();
                                        form.render();
                                        layer.close(index);
                                        layer.closeAll(); // 关闭所有弹层
                                        loadRoleData(); // 刷新表格数据
                                    }
                                });
                            } else {
                                layer.alert(res.test, {
                                    title: '操作失败',
                                    icon: 2
                                });
                            }
                        },
                        error: function (xhr, status, error) {
                            layer.closeAll('loading');
                            layer.alert('请求失败: ' + error, {
                                title: '错误',
                                icon: 2,
                                btn: ['确定']
                            });
                            console.error('AJAX请求失败:', status, error);
                            console.error('响应内容:', xhr.responseText);
                        }
                    });
                    return false;
                });

            });
    </script>
</body>
</html>